<?php

namespace Addons\OaSystem\Controller;
use Addons\OaSystem\Controller\OaDutyBaseController;

class OaRoleNodeController extends OaDutyBaseController
{
    var $model;

    function _initialize()
    {
        $this->model = $this->getModel('oa_role_node');
        parent::_initialize();

    }
    function lists(){
        $list_data = $this->_get_model_list( $this->model );
        $grids = $list_data['list_grids'];
        $data = array();

        $role = db_select('oa_role');
        foreach($role as $i => $r) {
            $data[$i]['role_id'] = $r['name'];
            $data[$i]['id'] = $r['id'];
            $role_node = db_select('oa_role_node', ['role_id'=>$r['id']]);
            foreach($role_node as $rn) {
                $node = db_find('oa_node', ['id'=>$rn['node_id']]);
                $data[$i]['node_id'] .= empty($data[$i]['node_id']) ? $node['name'] : '  /  '.$node['name'];
            }

            $editUrl = U('edit', array('id'=> $r['id']));
            $delUrl = U('del', array('id'=> $r['id']));
            $data[$i]['urls'] = "<a href=$editUrl>编辑</a>".'   '."<a class='confirm' href=$delUrl>删除</a>";

            if($role_node == NULL) {
                unset($data[$i]);
            }
        }
        $data = array_values($data);
        $this->assign('list_grids', $grids);
        $this -> assign('list_data', $data);
        $this->display();
    }

    function add(){
        if(IS_POST) {
            $data['appid'] = $this->appId;
            $data['role_id'] = $_POST['role_id'];
            $data['admin'] = $_POST['admin'];
            $data['read'] = $_POST['read'];
            $data['write'] = $_POST['write'];

            $nodes = db_select('oa_node');
            foreach ($nodes as $n) {
                if($_POST[$n['name']] == 1) {
                    $data['node_id'] = $n['id'];
                    if(db_find('oa_role_node', ['role_id'=>$data['role_id'], 'node_id'=>$data['node_id']])) {
                        $this->error('该角色功能关系已经存在, 请重新输入！');
                        return false;
                    }
                    M('oa_role_node')->add($data);
                }
            }

            $this->success ( '创建' . $model ['title'] . '成功！', U ( 'lists?model=' . $model ['name'], $this->get_param ));

        } else {
            $role = db_select('oa_role');
            $node = db_select('oa_node');
            $this->assign('role', $role);
            $this->assign('node', $node);
            $this->display('add');
        }

    }
    function edit()
    {
        if(IS_POST) {
            $data['appid'] = $this->appId;
            $data['role_id'] = $_POST['role_id'];
            $data['admin'] = $_POST['admin'];
            $data['read'] = $_POST['read'];
            $data['write'] = $_POST['write'];
            $nodes = db_select('oa_node');
            M('OaRoleNode')->where(array('role_id'=>$data['role_id']))->delete();
            foreach ($nodes as $n) {
                if($_POST[$n['name']] == 1) {
                    $data['node_id'] = $n['id'];
                    M('oa_role_node')->add($data);
                }
            }
            $this->success ( '修改' . $model ['title'] . '成功！', U ( 'lists?model=' . $model ['name'], $this->get_param ));
        }
        else {
            $map['id'] = I('get.id');
            $role = M('oaRole')->where($map)->select();
            $nodes = db_select('oa_node');
            for ($i = 0; $i < count($nodes); $i++) {
                $rn_map['role_id'] = I('get.id');
                $rn_map['node_id'] = $nodes[$i]['id'];
                $data = M('OaRoleNode')->where($rn_map)->find();
                if(empty($data)) {
                    $nodes[$i]['check'] = '';
                }else{
                    $nodes[$i]['check'] = 'checked';
                }
            }
            $this->assign('role', $role);
            $this->assign('node', $nodes);
            $this->display('edit');
        }
    }

    function del() {
        if(IS_POST) {
            myprint('$_POST = ', $_POST);
        }
    }
}